package de.dopichaj.labrador.index.own;


import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;

import de.dopichaj.labrador.index.FileSystemIndex;
import de.dopichaj.labrador.index.IndexVersionException;
import de.dopichaj.labrador.index.WritableIndex;
import de.dopichaj.labrador.index.backend.Dictionary;
import de.dopichaj.labrador.index.backend.DiskDictionary;
import de.dopichaj.labrador.index.backend.DiskInvertedList;
import de.dopichaj.labrador.index.backend.DiskInvertedListSearcher;
import de.dopichaj.labrador.index.backend.InvertedList;
import de.dopichaj.labrador.index.backend.InvertedListSearcher;
import de.dopichaj.labrador.search.NEXISearcher;
import de.dopichaj.labrador.util.Configuration;


public final class OwnFileSystemIndex extends FileSystemIndex implements OwnIndex {

    private final File dictionaryFile;
    private final File invertedListFile;

    /**
     * Create and open the index.
     */
    public OwnFileSystemIndex(File indexDirectory, String[] stopWords, Configuration configuration)
        throws IOException {

        super(indexDirectory, stopWords, configuration);
        this.dictionaryFile = new File(indexDirectory, "dict");
        this.invertedListFile = new File(indexDirectory, "il");
        final OutputStream out = new FileOutputStream(getConfigurationFile());
        configuration.save(out);
        out.close();
    }
    
    /**
     * Open the index for read-only access.
     */
    public OwnFileSystemIndex(File indexDirectory) throws IOException,
        IndexVersionException {

        super(indexDirectory);
        this.dictionaryFile = new File(indexDirectory, "dict");
        this.invertedListFile = new File(indexDirectory, "il");
    }

    public InvertedList getInvertedList() throws IOException {
        return new DiskInvertedList(indexDirectory,
                new RandomAccessFile(invertedListFile, "rw"),
                getConfiguration().getInt("run-length", 50000),
                false);
    }

    public RandomAccessFile getDictionaryFile() throws IOException {
        return new RandomAccessFile(dictionaryFile, "r");
    }
    
    public DataOutputStream getDictionaryOutputStream() throws FileNotFoundException  {
        return new DataOutputStream(new BufferedOutputStream(
            new FileOutputStream(dictionaryFile)));
    }

    public InvertedListSearcher getInvertedListSearcher() throws IOException {
        return new DiskInvertedListSearcher(new RandomAccessFile(invertedListFile, "r"));
    }

    @Override
    public NEXISearcher getNEXISearcher() throws IOException {
        return OwnSearcherFactory.getNEXISearcher(this, this);
    }

    public Dictionary getDictionary() {
        return new Dictionary();
    }

    @Override
    public WritableIndex getWritableIndex() throws IOException {
        return new OwnWritableIndex(this);
    }

    @Override
    public String getIndexVersion() {
        return "oi18";
    }

    public DiskDictionary getReadOnlyDictionary() throws IOException {
        return new DiskDictionary(getDictionaryFile());
    }

}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/